package com.efounder.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.efounder.mobilemanager.R;
import com.efounder.util.AppContext;

public class WithProgressLoadingWithUpdate {

    /*
     * 定义loadingDialog
     */
    private static Dialog loadingDialog;

	/*
     * 定义Appcontext全局变量
	 */
    //public static AppContext appContext = AppContext.getInstance();

    public static ProgressBar getBar() {
        return bar;
    }

    public static void setBar(ProgressBar bar) {
        WithProgressLoadingWithUpdate.bar = bar;
    }

    /*
         * 加载显示的文字
         */
    static ProgressBar bar;
    static ImageButton imageButton;
    static boolean isloading = true;

    public static CancelProgressListener cancelProgressListener;


    private static TextView down;
    private static TextView cancle;
    /**
     * 进度文字
     */
    private static TextView progresstext;
    /**
     * 左上角标题
     */
    private static TextView title;
    /**
     * 内容
     */
    private static TextView content;

    /*
     * 重载方法
     */
    public static void show(Context context) {
        Resources res = context.getResources();
        String loading = "加载中。。。";

        show(context, loading);
    }

    /*
     * 显示dialog
     */
    public static void show(Context context, String msg) {

        // 用来载入LinearLayout
        LayoutInflater layoutInflater = LayoutInflater.from(context);

        // 创建视图
        View v = layoutInflater.inflate(R.layout.withwaitingupdate, null);


        bar = (ProgressBar) v.findViewById(R.id.withwaitingupdate_progressbar);

        // 加载布局
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);


        loadingDialog = new Dialog(context, R.style.withwaitingupdate_loadingData_dialog);
        // 不可以用“返回键”取消
        loadingDialog.setCancelable(true);
        //loadingDialog.setCancelable(true);
        loadingDialog.setContentView(layout);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setOnCancelListener(new
                                                  DialogInterface.OnCancelListener() {
                                                      public void onCancel(DialogInterface dialog) {
                                                          if (null != cancelProgressListener)
                                                              cancelProgressListener.cancelProgress();
                                                      }
                                                  });


        //设置dialog的大小
        Window dialogWindow = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		/* lp.width = Detail_Util.dip2px(appContext, bar.getWidth())+450;
		 lp.height =Detail_Util.dip2px(appContext,  bar.getHeight())+350;*/
        DisplayMetrics dm = new DisplayMetrics();
        ;//屏幕分辨率容器
        WindowManager windowManager = (WindowManager) AppContext.getInstance()
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;
        lp.width = (int) (screenWidth / 1.2);
        //  lp.height = screenHeight/3;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

		/*loadingDialog.getWindow().setType(
				WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);*/
        loadingDialog.show();


        //初始化布局
        down = (TextView) layout.findViewById(R.id.withwaitingupdate_down);
        cancle = (TextView) layout.findViewById(R.id.withwaitingupdate_cancel);
        progresstext = (TextView) layout.findViewById(R.id.progresstext);
        title = (TextView) layout.findViewById(R.id.withwaitingupdat_title);
        content = (TextView) layout.findViewById(R.id.withwaitingupdat_content);

    }

	/*
	 * 取消dialog
	 */

    public static void dismiss() {
        if (loadingDialog != null)
            loadingDialog.dismiss();
    }

    public static Dialog getDialog() {
        return loadingDialog;
    }

    public static void setProgress(int i) {
        bar.setProgress(i);
    }

    public interface CancelProgressListener {
        public void cancelProgress();

        public void startProgress();

    }

    public static void setCancelProgressListener(CancelProgressListener cpl) {
        cancelProgressListener = cpl;
    }

    public static TextView getDown() {
        return down;
    }

    public static void setDown(TextView down) {
        WithProgressLoadingWithUpdate.down = down;
    }

    public static TextView getCancle() {
        return cancle;
    }

    public static void setCancle(TextView cancle) {
        WithProgressLoadingWithUpdate.cancle = cancle;
    }

    public static TextView getProgresstext() {
        return progresstext;
    }

    public static void setProgresstext(TextView progresstext) {
        WithProgressLoadingWithUpdate.progresstext = progresstext;
    }

    public static TextView getTitle() {
        return title;
    }

    public static void setTitle(TextView title) {
        WithProgressLoadingWithUpdate.title = title;
    }

    public static TextView getContent() {
        return content;
    }

    public static void setContent(TextView content) {
        WithProgressLoadingWithUpdate.content = content;
    }

}
